/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.xmlservice
{
	import com.screenworker.compactmvc.extensions.xmlservice.interfaces.IXMLService;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class XMLService extends Model implements IXMLService
	{
		private var _dataLoader:URLLoader;
		private var _data:XML;
		
		public function XMLService(name:String, controller:Controller)
		{
			super(name, controller);
			
			_dataLoader = new URLLoader();
			_dataLoader.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler );
			_dataLoader.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
			_dataLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			
		}
		
		public function reset():void
		{		
			_dataLoader.close();
			_dataLoader.removeEventListener(ProgressEvent.PROGRESS, loaderProgressHandler );
			_dataLoader.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
			_dataLoader.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
			_dataLoader = null;
			_data = null;
		}
		
		override public function removeHandler():void
		{
			reset();
		}
		
		/**
		 * load
		 * @param url
		 * 
		 */		
		protected function load(url:String):void 
		{
			var request:URLRequest = new URLRequest(url);	
			try 
			{	
             	_dataLoader.load(request);	
            } 
            catch (error:Error) 
            {
            	errorHandler('Unable to load requested document:' + url );
            }		
        }
        
		private function loaderProgressHandler( e:ProgressEvent ):void
		{
			progressHandler(e.bytesLoaded/e.bytesTotal);
		}
		
		private function loaderIOErrorHandler(e:IOErrorEvent):void 
		{
			IOErrorHandler(e.text);
        }
        
		private function loaderCompleteHandler(e:Event):void 
		{	
			var xml:XML = new XML( e.target.data );
			if(xml == null )
			{
				errorHandler('XML not Valid!');
				return;
			}			
			xml.ignoreWhitespace = true;
			_data = xml;
			completeHandler();
		}
		
		public function progressHandler(progress:Number):void
		{
			//overriden by subclass
		}
		
		public function IOErrorHandler(message:String):void 
		{
			//overriden by subclass
        }
        
        public function errorHandler(message:String):void 
		{
			//overriden by subclass
        }
        
        public function completeHandler():void 
		{
			//overriden by subclass
		}
		
		public function get data():XML
		{
			return _data;
		}
		
		public function set data(value:XML):void
		{
			_data = value;
		}
		
	}
}
